import {editRemoteUserInfo, getSmsCode, validateSmsCode, editRemoteShangjiaUserInfo} from '../../api/index'
import {isEmptyField} from '../../utils/util'
import {vModel} from '../../utils/v-model'
import CAR_NUM_LIST from '../../utils/carAddr'
import {shareApp} from '../../utils/shareApp'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    role: '',
    areaOptions: CAR_NUM_LIST,
    areaIndex: 0,
    codeTimer: 120, // 计时
    smsCode: '',
    phone: '',
    form: {
      name: '',
      id_number: '',
      car_number: ''
    },
    form2: {
      name: '',
      address: ''
    },
    selectAreaPopupVisible: false
  },
  // 输入手机号提示
  bindShowInputPhone() {
    wx.showModal({
      title: '提示',
      content: '手机号用于验证用户真实身份哦~',
      showCancel: false,
      confirmText: '好的'
    })
  },
  // v-model
  bindInput (e) {
    vModel(this, e)
  },
  // 计时器
  setInterval () {
    let _this = this
    let _timer = setInterval(() => {
      this.setData({
        codeTimer: _this.data.codeTimer - 1
      })
    }, 1000)
    setTimeout(() => {
      clearInterval(_timer)
      this.setData({
        codeBtnDisabled: false,
        codeTimer: 60
      })
    }, 120000)
  },
  // 发送验证码
  bindSendCode () {
    let _this = this
    let _phone = _this.data.phone.trim()
    if (_phone === '') {
      wx.showToast({
        icon: 'none',
        title: '请输入手机号'
      })
      return
    }
    if (!(/1\d{10}/.test(_phone))) {
      wx.showToast({
        icon: 'none',
        title: '手机号不正确'
      })
      return
    }
    getSmsCode(_phone).then(res => {
      console.log(res)
      _this.setInterval()
      _this.setData({
        codeBtnDisabled: true
      })
      wx.showToast({
        title: '已发送'
      })
    }).catch(err => {
      wx.showToast({
        icon: 'none',
        title: err.data.info
      })
    })
  },
  bindNextStep () {
    let _this = this
    let _data = _this.data
    let _form = _data.form
    // let _areaAbbrName = _data.areaOptions[_data.areaIndex]
    if (isEmptyField(_data.phone)) {
      wx.showToast({
        icon: 'none',
        title: '请输入手机号'
      })
      return
    } else {
      if (!(/1\d{10}/.test(_data.phone))) {
        wx.showToast({
          icon: 'none',
          title: '手机号不正确'
        })
        return
      }
    }
  
    if (isEmptyField(_data.smsCode)) {
      wx.showToast({
        icon: 'none',
        title: '请输入验证码'
      })
      return
    }
  
    if (isEmptyField(_form.name)) {
      wx.showToast({
        icon: 'none',
        title: '请输入姓名'
      })
      return
    }
  
    if (isEmptyField(_form.id_number)) {
      wx.showToast({
        icon: 'none',
        title: '请输入身份证号'
      })
      return
    }
  
    if (isEmptyField(_form.car_number)) {
      wx.showToast({
        icon: 'none',
        title: '请输入车牌号'
      })
      return
    }
    //  _areaAbbrName +
    let _params = Object.assign({}, _form, {car_number: _form.car_number, phone: _data.phone})
    // 验证短信， 并提交信息
    validateSmsCode({phone: _params.phone, code: _data.smsCode}).then(res => {
      editRemoteUserInfo(_params).then(res => {
        wx.navigateTo({
          url: '/pages/init/step3?role=' + _data.role,
        })
      }).catch(err => {
        wx.showToast({
          icon: 'none',
          title: err.data.info
        })
      })
    }).catch(err => {
      wx.showModal({
        showCancel: false,
        title: '提示',
        content: err.data.info
      })
    })
  },
  bindNextStep2 () {
    let _this = this
    let _data = _this.data
    let _form = _data.form2
    // let _areaAbbrName = _data.areaOptions[_data.areaIndex]
    if (isEmptyField(_data.phone)) {
      wx.showToast({
        icon: 'none',
        title: '请输入手机号'
      })
      return
    } else {
      if (!(/1\d{10}/.test(_data.phone))) {
        wx.showToast({
          icon: 'none',
          title: '手机号不正确'
        })
        return
      }
    }
    
    if (isEmptyField(_form.name)) {
      wx.showToast({
        icon: 'none',
        title: '请输入姓名'
      })
      return
    }
    
    if (isEmptyField(_form.address)) {
      wx.showToast({
        icon: 'none',
        title: '请输入地址'
      })
      return
    }
    
    // 提交商家信息
    let _params = Object.assign({}, _form, {car_number: _form.car_number, phone: _data.phone})
    // 验证短信， 并提交信息
    validateSmsCode({phone: _params.phone, code: _data.smsCode}).then(res => {
      wx.showLoading({
        title: '正在提交...'
      })
      editRemoteShangjiaUserInfo(_params).then(res => {
        setTimeout(() => {
          wx.hideLoading()
          wx.redirectTo({
            url: '/pages/init/feedback?state=submit',
          })
        }, 1500)
      }).catch(err => {
        wx.showToast({
          icon: 'none',
          title: err.data.info
        })
      })
    }).catch(err => {
      wx.showModal({
        showCancel: false,
        title: '提示',
        content: err.data.info
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      role: options.role
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return shareApp()
  }
})
